package br.com.async.core.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class SendMail {

	private final String emailAddressee;

	private final String emailData;

	private final String subject;


	
	public SendMail(final String emailAddressee, final String subject,
			final String emailData) {

		this.emailAddressee = emailAddressee;

		this.emailData = emailData;

		this.subject = subject;

	}

	
	public void sendEmail() throws FailSendMailException {
		MailThread mt = new MailThread();
		Thread mailThread = new Thread(mt);
		mailThread.start();
	}

	
	private class MailThread implements Runnable {

		@Override
		public void run() {
			sendCM();
		}

		private synchronized void sendCM() {

			final HtmlEmail htmlEmail = new HtmlEmail();

			try {

				htmlEmail.setHostName("smtp.gmail.com");
				htmlEmail.setTLS(true);
				htmlEmail.setCharset("UTF-8");
				htmlEmail.addTo(emailAddressee);
				htmlEmail.setFrom("bookmarkscariri@yahoo.com.br", "Bookmarks Cariri");
				htmlEmail.setAuthentication("bookmarkscariri@yahoo.com.br", "Minhasenha123");
				htmlEmail.setSubject(subject);
				htmlEmail.setHtmlMsg(emailData);

				// Efetua envio do email
				htmlEmail.send();

			} catch (EmailException e) {
				e.printStackTrace();
				//throw new FailSendMailException();
			}

		}

	}
}
